﻿using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using WebApi.Common;
using WebApi.Common.MiddleWare;
using WXQ.BusinessCore.systemmanage;
using WXQ.Enties;
using WXQ.Enties.CommonObj;
using WXQ.InOutPutEntites.Input;
using WXQ.InOutPutEntites.Input.SystemManage.Department;
using WXQ.InOutPutEntites.Output;
using UserOp = WebApi.Common.HelpOp.UserOp;

namespace WebApi.Controllers.systemmanage
{
    /// <summary>
    /// 部门模块
    /// </summary>
    [Route("Api/SystemManage/Department")]
    public class DepartmentController : BaseController
    {
        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Log]
        [Route("InsertDepartment")]
        public JsonResult InsertDepartment([FromBody] InsertDepartmentInput model)
        {
            var result = new ResponseResult();

            var userId = UserOp.GetUserId(User);
            var op = new DepartmentOp(userId);

            var r = new Department
            {
                AddDateTime = DateTime.Now,
                AddUser = User.Identity?.Name,
                Description = model.Description,
                ParentId = model.ParentId,
                DepartmentName = model.DepartmentName
            };

            var rv = op.InsertDepartment(r);

            if (!rv)
            {
                result.Code = ResponseResultMessageDefine.OpLost;
                result.Errors.Add(ResponseResultMessageDefine.OpLostMessage);
            }

            return Json(result);
        }

        /// <summary>
        /// 修改部门
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Log]
        [Route("UpdateDepartment")]
        public JsonResult UpdateDepartment([FromBody] UpdateDepartmentInput model)
        {
            var result = new ResponseResult();

            var userId = UserOp.GetUserId(User);
            var op = new DepartmentOp(userId);

            var r = new Department
            {
                DepartmentId = model.DepartmentId,
                UpdateDateTime = DateTime.Now,
                UpdateUser = User.Identity?.Name,
                Description = model.Description,
                ParentId = model.ParentId,
                DepartmentName = model.DepartmentName
            };

            var rv = op.UpdateDepartment(r);

            if (!rv)
            {
                result.Code = ResponseResultMessageDefine.OpLost;
                result.Errors.Add(ResponseResultMessageDefine.OpLostMessage);
            }

            return Json(result);
        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="DepartmentId"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Log]
        [Route("DeleteDepartmentByDepartmentId")]
        public JsonResult DeleteDepartmentByDepartmentId([FromQuery] int DepartmentId)
        {
            var result = new ResponseResult();

            if (DepartmentId < 1)
            {
                result.Code = ResponseResultMessageDefine.ParaError;
                result.Errors.Add("部门id错误");
            }
            else
            {
                var userId = UserOp.GetUserId(User);
                var op = new DepartmentOp(userId);

                var rv = op.DeleteDepartmentByDepartmentId(DepartmentId);

                if (!rv)
                {
                    result.Code = ResponseResultMessageDefine.OpLost;
                    result.Errors.Add(ResponseResultMessageDefine.OpLostMessage);
                }
            }

            return Json(result);
        }

        /// <summary>
        /// 查找部门
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetDepartmentList")]
        [Log]
        public JsonResult GetDepartmentList([FromBody] GetDepartmentListInput model)
        {
            var result = new ResponseResult();

            var op = new DepartmentOp();

            var pagemodel = model.Page.Adapt<PageInput, PageModel>();
            var rv = op.GetDepartmentList(model.DepartmentName, model.ParentId, model.Page.PageIndex,
                model.Page.PageSize);

            if (rv == null)
            {
                result.Code = ResponseResultMessageDefine.OpLost;
                result.Errors.Add(ResponseResultMessageDefine.OpLostMessage);
            }
            else
            {
                result.Data = rv;
            }

            return Json(result);
        }

        /// <summary>
        /// 查找用户所在的部门
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetDepartmentListByUserId")]
        [Log]
        public JsonResult GetDepartmentListByUserId([FromBody] GetDepartmentListByUserIdInput model)
        {
            var result = new ResponseResult();

            var op = new DepartmentOp();

            var pagemodel = model.Page.Adapt<PageInput, PageModel>();
            var rv = op.GetDepartmentListByUserId(model.UserId, model.Page.PageIndex, model.Page.PageSize);

            if (rv == null)
            {
                result.Code = ResponseResultMessageDefine.OpLost;
                result.Errors.Add(ResponseResultMessageDefine.OpLostMessage);
            }
            else
            {
                result.Data = rv;
            }

            return Json(result);
        }

        /// <summary>
        /// 查找当前用户所在的部门
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetDepartmentListForCurrentUser")]
        [Log]
        [Authorize]
        public JsonResult GetDepartmentListForCurrentUser()
        {
            var result = new ResponseResult();

            var userId = UserOp.GetUserId(User);

            if (userId <= 0)
            {
                result.Code = ResponseResultMessageDefine.ParaError;
                result.Errors.Add(ResponseResultMessageDefine.OpLostMessage);
            }
            else
            {
                var op = new DepartmentOp();

                var rv = op.GetDepartmentListByUserId(userId, 1, 200);

                if (rv == null)
                {
                    result.Code = ResponseResultMessageDefine.OpLost;
                    result.Errors.Add(ResponseResultMessageDefine.OpLostMessage);
                }
                else
                {
                    result.Data = rv;
                }
            }

            return Json(result);
        }

        /// <summary>
        /// 查找当前用户所在的部门和子部门
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetDepartmentAndSubDepartmentForCurrentUser")]
        [Log]
        [Authorize]
        public JsonResult GetDepartmentAndSubDepartmentForCurrentUser()
        {
            var result = new ResponseResult();

            var userId = UserOp.GetUserId(User);

            if (userId <= 0)
            {
                result.Code = ResponseResultMessageDefine.ParaError;
                result.Errors.Add(ResponseResultMessageDefine.OpLostMessage);
            }
            else
            {
                var op = new DepartmentOp();

                var rv = op.GetDepartmentAndSubDepartmentByUserId(userId, 1, 200);

                if (rv == null)
                {
                    result.Code = ResponseResultMessageDefine.OpLost;
                    result.Errors.Add(ResponseResultMessageDefine.OpLostMessage);
                }
                else
                {
                    result.Data = rv;
                }
            }

            return Json(result);
        }

        /// <summary>
        /// 查找部门中的用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetUserListByDepartmentId")]
        public JsonResult GetUserListByDepartmentId([FromBody] GetUserListByDepartmentIdInput model)
        {
            var result = new ResponseResult();

            var op = new DepartmentOp();

            var pagemodel = model.Page.Adapt<PageInput, PageModel>();
            var rv = op.GetUserListByDepartmentId(model.DepartmentId, model.Page.PageIndex, model.Page.PageSize);

            if (rv == null)
            {
                result.Code = ResponseResultMessageDefine.OpLost;
                result.Errors.Add(ResponseResultMessageDefine.OpLostMessage);
            }
            else
            {
                result.Data = rv;
            }

            return Json(result);
        }

        /// <summary>
        /// 添加部门的用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("AddUserForDepartment")]
        [Log]
        [Authorize]
        public JsonResult AddUserForDepartment([FromBody] ModifyUserForDepartmentInput model)
        {
            var result = new ResponseResult();

            var userId = UserOp.GetUserId(User);
            var op = new DepartmentOp(userId);

            var rv = op.AddUserForDepartment(model.DepartmentId, model.UserIds);

            if (!rv)
            {
                result.Code = ResponseResultMessageDefine.OpLost;
                result.Errors.Add(ResponseResultMessageDefine.OpLostMessage);
            }
            else
            {
                result.Data = rv;
            }

            return Json(result);
        }

        /// <summary>
        /// 移除部门中的用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("RemoveUserForDepartment")]
        [Log]
        [Authorize]
        public JsonResult RemoveUserForDepartment([FromBody] ModifyUserForDepartmentInput model)
        {
            var result = new ResponseResult();

            var userId = UserOp.GetUserId(User);
            var op = new DepartmentOp(userId);

            var rv = op.DeleteUserForDepartment(model.DepartmentId, model.UserIds);

            if (!rv)
            {
                result.Code = ResponseResultMessageDefine.OpLost;
                result.Errors.Add(ResponseResultMessageDefine.OpLostMessage);
            }
            else
            {
                result.Data = rv;
            }

            return Json(result);
        }
    }
}